package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.impl.cookie.RFC2965DiscardAttributeHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@RestController("adminDishController")
@RequestMapping("/admin/dish")
@Slf4j
@CrossOrigin
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;



    @GetMapping("/list")
    @ApiOperation("通过分类id查询菜品")
    public Result<List<Dish>> listByCategoryId(Long categoryId){
        log.info("通过分类id查询菜品：{}",categoryId);
        List<Dish> list=dishService.listByCategoryId(categoryId);
        return Result.success(list);
    }


    @GetMapping("/{id}")
    @ApiOperation("通过菜品id查询菜品")
    public Result<DishVO> getById(@PathVariable Long id){
        log.info("通过菜品id查询菜品：{}",id);
        DishVO dishVO=dishService.getByIdWithFlavor(id);

        return Result.success(dishVO);

    }


    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        log.info("分页查询：{}",dishPageQueryDTO);
        PageResult pageResult=dishService.page(dishPageQueryDTO);

        return Result.success(pageResult);
    }



    @PostMapping
    @ApiOperation("新增菜品")
    public Result<String> insert(@RequestBody DishDTO dishDTO){
        log.info("新增菜品：{}",dishDTO);

        dishService.insertWithFlavor(dishDTO);

        //清理缓存
        cleanCache("dish_"+dishDTO.getCategoryId());


        return Result.success();
    }


    @DeleteMapping
    @ApiOperation("批量删除菜品")
    //以集合形式接收参数，必须加上@RequestParam
    public Result<String> deleteBatch(@RequestParam List<Long> ids){
        log.info("批量删除菜品：ids:{}",ids);

        dishService.deleteBatch(ids);

        //可能影响多个不确定的key，直接全删掉
        cleanCache("dish_*");


        return Result.success();
    }

    @PutMapping
    @ApiOperation("修改菜品")
    public Result<String> update(@RequestBody DishDTO dishDTO){

        dishService.update(dishDTO);

        cleanCache("dish_*");

        return Result.success();
    }


    @PostMapping("/status/{status}")
    @ApiOperation("起售停售")

    public Result<String> modifyStatus(@PathVariable Integer status,Long id){

        dishService.modifyStatus(status,id);

        cleanCache("dish_*");



        return Result.success();
    }


    private void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
